package com.xxkj.drugtraceability.ui.prescriptiondrugback

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.xxhis.drugtraceability.base.BaseViewModel
import com.xxkj.drugtraceability.bean.*
import com.xxkj.drugtraceability.net.Result
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:15
 * description:
 */
class PrescriptionDrugBackViewModel(application: Application, private val repository: PrescriptionDrugBackRepository):BaseViewModel(application) {
    var prescriptioninfo = MutableLiveData<List<PrescriptionInfoBean>?>()
    var topList = MutableLiveData<List<PatientDrugData>>()
    var topData = mutableListOf<PatientDrugData>()
    var baseUiModel = MutableLiveData<BaseUiModel<Boolean>>()
    var barcode = ""
    var Prescriptionbarcode = ""
    //顶部被选中的处方数据
    var topIndex = 0
    //当前选中的处方编码
    var nowRxno = ""

    fun queryPrescriptionInfoList(){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                //获取下面具体数据
                val resultQuery = repository.getPreDrugRefundList(
                    Gson().toJson(
                        PrescriptionRequest(
                            nowRxno,
                            "1"
                        )
                    )
                )
                if (resultQuery is Result.Success) {
                    Prescriptionbarcode = nowRxno
                    if (resultQuery.data.data.isNotEmpty()) {
                        prescriptioninfo.postValue(resultQuery.data.data)
                        if (topData.isEmpty()){
                            //造一条顶部的数据，不然没有数据啊
                          val data =  PatientDrugData(
                                pharCertno = resultQuery.data.data[0].pharCertno,
                                prscDrCertno = resultQuery.data.data[0].prscDrCertno,
                                rxno = resultQuery.data.data[0].rxno,
                                prscDrName = resultQuery.data.data[0].prscDrName,
                                prescriptionTime = resultQuery.data.data[0].prescriptionTime,
                                pharmacyName = resultQuery.data.data[0].pharmacyName,
                                stooutNo = resultQuery.data.data[0].stooutNo,
                                pharPracCertNo = resultQuery.data.data[0].pharPracCertNo,
                                psnName = resultQuery.data.data[0].psnName,
                                prscDrCertType = resultQuery.data.data[0].prscDrCertType,
                                rxCircFlag = resultQuery.data.data[0].rxCircFlag,
                                pharName = resultQuery.data.data[0].pharName,
                                dataType = resultQuery.data.data[0].dataType,
                                patientId = resultQuery.data.data[0].patientId,
                                pharmacyCode = resultQuery.data.data[0].pharmacyCode,
                                pharCertType = resultQuery.data.data[0].pharCertType,
                            )
                            topData.add(data)
                            topList.postValue(topData)
                        }

                    } else {
                        prescriptioninfo.postValue(null)
                    }
                    baseUiModel.postValue(BaseUiModel(showLoading = false))
                } else if (resultQuery is Result.Error) {
                    baseUiModel.postValue(
                        BaseUiModel(
                            showLoading = false,
                            showError = resultQuery.errorData ?: resultQuery.exception.message
                        )
                    )
                }
            }}
    }

    /**
     * 保存数据
     */
    fun finishScanBackDrugCode(finishRequest: FinishRequest){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                baseUiModel.postValue(BaseUiModel(showLoading = true))
                val result = repository.finishScanBackDrugCode(GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(finishRequest))
                if (result is Result.Success){
                    baseUiModel.postValue(BaseUiModel(showLoading = false,showSuccess = true))
                    barcode = ""
                    prescriptioninfo.postValue(mutableListOf())
                    Prescriptionbarcode = ""
//                    //刷新当前页面
//                    queryPrescriptionInfoList()
                }else if (result is Result.Error){
                    baseUiModel.postValue(BaseUiModel(showLoading = false,showError = result.errorData?:result.exception.message))
                }
            }
        }
    }

}